package com.Mobile.Action;

import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import net.sf.json.JSONArray;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import com.Entity.Media;
import com.Entity.Mediacomment;
import com.Service.MediaCommentService;
import com.Service.MediaService;

public class MobileMediaCommentAction {
	
	private static final Logger logger = Logger.getLogger(MobileMediaCommentAction.class);

	private Mediacomment mediacomment=new Mediacomment();
	private Mediacomment commentbean=new Mediacomment();
	@Autowired
	private MediaCommentService mediacommentservice;
	private List<Mediacomment> mediacommentlist;
	private int flag=0;
	JSONObject returnjson=new JSONObject();		
	HttpServletResponse response = ServletActionContext.getResponse();
	
	public void listComment() throws Exception {
		String videoid=ServletActionContext.getRequest().getParameter("videoid");
		videoid =new String(videoid.getBytes("ISO-8859-1"), "UTF-8");
		logger.info(videoid);
		String row=ServletActionContext.getRequest().getParameter("row");
		row =new String(row.getBytes("ISO-8859-1"), "UTF-8");
		logger.info(row);
		String page=ServletActionContext.getRequest().getParameter("page");
		page =new String(page.getBytes("ISO-8859-1"), "UTF-8");
		logger.info(page);
		response.setContentType("application/json;charset=utf-8"); 
		PrintWriter out = response.getWriter();	
		if(null==videoid||videoid.isEmpty()||null==row||row.isEmpty()||null==page||page.isEmpty())
		{
			logger.info("error");
			returnjson.put("flag", flag);
		}
		else{
			mediacommentlist=this.mediacommentservice.findCommentByVideoId(Long.parseLong(videoid),Integer.parseInt(page),Integer.parseInt(row));
			JSONArray jsonbean=JSONArray.fromObject(mediacommentlist);
			flag=1;
			returnjson.put("flag", flag);	
			returnjson.put("info", jsonbean);
		}
		out.println(returnjson.toString());
		out.flush();
		out.close();
	}
	
	public void addComment() throws Exception {
		String videoid=ServletActionContext.getRequest().getParameter("videoid");
		videoid =new String(videoid.getBytes("ISO-8859-1"), "UTF-8");
		logger.info(videoid);
		String authorid=ServletActionContext.getRequest().getParameter("authorid");
		authorid =new String(authorid.getBytes("ISO-8859-1"), "UTF-8");
		logger.info(authorid);
		String authorname=ServletActionContext.getRequest().getParameter("authorname");
		authorname =new String(authorname.getBytes("ISO-8859-1"), "UTF-8");
		logger.info(authorname);
		String commenttext=ServletActionContext.getRequest().getParameter("commenttext");
		commenttext =new String(commenttext.getBytes("ISO-8859-1"), "UTF-8");
		logger.info(commenttext);
		String isreplycomment=ServletActionContext.getRequest().getParameter("isreplycomment");
		isreplycomment =new String(isreplycomment.getBytes("ISO-8859-1"), "UTF-8");
		logger.info(isreplycomment);
		String originalid=ServletActionContext.getRequest().getParameter("originalid");
		originalid =new String(originalid.getBytes("ISO-8859-1"), "UTF-8");
		logger.info(originalid);
		long commentNum=this.mediacommentservice.countTotalCommentByVideoId(Long.parseLong(videoid));
		response.setContentType("application/json;charset=utf-8"); 
		PrintWriter out = response.getWriter();	
		if(null==videoid||videoid.isEmpty()||null==authorid||authorid.isEmpty()
				||null==authorname||authorname.isEmpty()||null==commenttext||commenttext.isEmpty()
				||null==isreplycomment||isreplycomment.isEmpty())
		{
			logger.info("error");
			returnjson.put("flag", flag);
		}
		else{
			if(isreplycomment.equals("0")){
				logger.info("isreplycomment=0");
				mediacomment.setVideoId(Long.parseLong(videoid));
				mediacomment.setAuthorId(Integer.parseInt(authorid));
				mediacomment.setAuthorName(authorname);
				mediacomment.setCommentText(commenttext);
				mediacomment.setCommentTime(new Date());
				mediacomment.setIsReplyComment(0);
				mediacomment.setFloorNum((int)commentNum+1);
				this.mediacommentservice.save(mediacomment);
				flag=1;
				returnjson.put("flag", flag);
			}
			else if(isreplycomment.equals("1")){
				logger.info("isreplycomment=1");
				commentbean=this.mediacommentservice.getById(Long.parseLong(originalid));
				logger.info("get success");
				mediacomment.setVideoId(Long.parseLong(videoid));
				mediacomment.setAuthorId(Integer.parseInt(authorid));
				mediacomment.setAuthorName(authorname);
				mediacomment.setCommentText(commenttext);
				mediacomment.setCommentTime(new Date());
				mediacomment.setIsReplyComment(1);
				mediacomment.setOriginalCommentorName(commentbean.getAuthorName());
				mediacomment.setOriginalCommentId(commentbean.getAuthorId());
				mediacomment.setOriginalCommentText(commentbean.getCommentText());
				mediacomment.setOriginalReplyTime(commentbean.getCommentTime());
				mediacomment.setFloorNum((int)commentNum+1);
				this.mediacommentservice.save(mediacomment);
				flag=1;
				returnjson.put("flag", flag);
			}
			else{
				logger.info("isreplycomment=?");
				returnjson.put("flag", flag);
			}					
		}
		out.println(returnjson.toString());
		out.flush();
		out.close();
	}


}
